package org.vt.ece4564.pcw.textinclass;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.app.Activity;
import android.content.Intent;

public class PostContactsActivity extends Activity implements OnItemClickListener, OnClickListener {
	
	private Cookie cookie_;
	private ListView contactsListView_;
	private Button uploadContactsButton_;
	private ProgressBar progressBar_;
	
	private List<String> names_;
	private List<String> numbers_;
	private List<Integer> excludedIndices_ = new ArrayList<Integer>();
	ArrayAdapter<String> adapter_;
	
	static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
        ContactsContract.Data.DISPLAY_NAME, Phone.NUMBER};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_contacts);
        
        Intent intent = getIntent();
        
        String cookieName = intent.getStringExtra("cookieName");
        String cookieValue = intent.getStringExtra("cookieValue");
        
        cookie_ = new BasicClientCookie(cookieName, cookieValue);
        
        contactsListView_ = (ListView) findViewById(R.id.postContactsListView);
        contactsListView_.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        contactsListView_.setOnItemClickListener(this);
        
        uploadContactsButton_ = (Button) findViewById(R.id.postContactsButton);
        uploadContactsButton_.setOnClickListener(this);
        
        progressBar_ = (ProgressBar) findViewById(R.id.postContactsProgressBar);
        progressBar_.setEnabled(false);
        progressBar_.setVisibility(View.INVISIBLE);
        
        Log.d("here2", "...");
        
        adapter_ = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
        
        LoadContacts loadContacts = new LoadContacts(this);
        loadContacts.execute();
        
    }
    
    public void setContacts(List<String> names, List<String> numbers) {
    	names_ = names;
    	numbers_ = numbers;
    	
    	for (String name : names_) {
    		adapter_.add(name);
    	}
    	
    	contactsListView_.setAdapter(adapter_);
    	
    	for (int i = 0; i < contactsListView_.getCount(); i++) {
    		contactsListView_.setItemChecked(i, true);
    	}
    	
    }

	public void onClick(View v) {
		
		uploadContactsButton_.setEnabled(false);
		progressBar_.setVisibility(View.VISIBLE);
		
		List<String> namesToAdd = new ArrayList<String>();
		List<String> numbersToAdd = new ArrayList<String>();
		
		for (int i = 0; i < names_.size(); i++) {
			if (!excludedIndices_.contains(Integer.valueOf(i))) {
				namesToAdd.add(names_.get(i));
				numbersToAdd.add(numbers_.get(i));
			}
		}
		
		PostContactsRequest postContactsRequest = new PostContactsRequest(namesToAdd,
														numbersToAdd, cookie_, this);
		postContactsRequest.execute();
		
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		if (!excludedIndices_.remove(Integer.valueOf(position))) {
			excludedIndices_.add(Integer.valueOf(position));
		}
	}
	
	public void resetScreen() {
		progressBar_.setVisibility(View.INVISIBLE);
		uploadContactsButton_.setEnabled(true);
	}
    
}
